package com.ems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ems.entity.DeviceEntity;
import com.ems.mapper.DeviceMapper;
import com.ems.service.IDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ems.util.SnowflakeIdWorker;
import com.ems.util.StateEnum;
import com.ems.util.StateException;
import com.ems.vo.DeviceVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 设备 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2024-05-08
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, DeviceEntity> implements IDeviceService {

    @Resource
    private DeviceMapper deviceMapper;

    @Override
    public DeviceVo saveSingle ( DeviceVo deviceVo ) {
        if ( deviceVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        DeviceEntity deviceEntity = new DeviceEntity ();
        BeanUtils.copyProperties ( deviceVo, deviceEntity );
        deviceEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        deviceEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( deviceEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return deviceVo;
    }

    @Override
    public List<DeviceVo> queryList ( DeviceVo deviceVo ) {
        QueryWrapper<DeviceEntity> queryWrapper = new QueryWrapper<> ();
        if ( deviceVo.getType () != null ) {
            queryWrapper.eq ( "type", deviceVo.getType () );
        }
        if ( StringUtils.isNotBlank ( deviceVo.getStationId () ) ) {
            queryWrapper.eq ( "stationId", deviceVo.getStationId () );
        }
        List<DeviceEntity> deviceEntities = baseMapper.selectList ( queryWrapper );
        List<DeviceVo> deviceVos = new ArrayList<> ();
        for ( DeviceEntity deviceEntity : deviceEntities ) {
            DeviceVo vo = new DeviceVo ();
            BeanUtils.copyProperties ( deviceEntity, vo );
            deviceVos.add ( vo );
        }
        return deviceVos;
    }

    @Override
    public DeviceVo querySingle ( DeviceVo deviceVo ) {
        QueryWrapper<DeviceEntity> queryWrapper = new QueryWrapper<> ();
        if ( StringUtils.isNotBlank ( deviceVo.getId () ) ) {
            queryWrapper.eq ( "id", deviceVo.getId () );
        }
        if ( StringUtils.isNotBlank ( deviceVo.getUserName () ) ) {
            queryWrapper.eq ( "userName", deviceVo.getUserName () );
        }
        if ( StringUtils.isNotBlank ( deviceVo.getPassword () ) ) {
            queryWrapper.eq ( "password", deviceVo.getPassword () );
        }
        if ( StringUtils.isNotBlank ( deviceVo.getDeviceAddress () ) ) {
            queryWrapper.eq ( "deviceAddress", deviceVo.getDeviceAddress () );
        }
        DeviceEntity deviceEntity = baseMapper.selectOne ( queryWrapper );
        BeanUtils.copyProperties ( deviceEntity, deviceVo );
        return deviceVo;
    }

    @Override
    public DeviceVo updateSingle ( DeviceVo deviceVo ) {
        deviceVo.setChangeTime ( new Date () );
        deviceMapper.updateSingle ( deviceVo );
        return deviceVo;
    }

}
